﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    private Vector3 m_lastInput = Vector3.zero;

    private GridMove m_grid_move;

    private int m_player_hp;

    private float state_duration;

    private bool is_invincible;

    private GameControl game_ctrl;
    // Start is called before the first frame update
    void Start()
    {
        is_invincible = false;
        m_grid_move = GetComponent<GridMove>();

        m_player_hp = GameControl.MaxHp;
        game_ctrl = GameObject.FindWithTag("GameCtrl").GetComponent<GameControl>();
    }

    // Update is called once per frame
    void Update()
    {
        switch (m_player_state)
        {
            case STATE.INVINCIBLE:
                state_duration += Time.deltaTime;
                if (state_duration >= 20)
                {
                    is_invincible = false;
                    m_player_state = STATE.NORMAL;
                }

                break;
            case STATE.ACCELERATE:
                state_duration += Time.deltaTime;
                if (state_duration >= 30)
                {
                    is_invincible = false;
                    m_player_state = STATE.NORMAL;
                    m_grid_move.ChangeSpeed(1);
                }
                break;
            case STATE.DEAD:
                game_ctrl.GameOver();
                break;
        }
    }
    public enum STATE
    {
        NORMAL,

        INVINCIBLE,     //无敌
        ACCELERATE,     //加速
        RESURRECTION,   //复活


        DEAD
    };

    private STATE m_player_state = STATE.NORMAL;

    Vector3 GetMoveDirection()
    {
        Vector3 direction;
        float h = Input.GetAxis("Horizontal");      //当按下左或右箭头时触发
        float v = Input.GetAxis("Vertical");        //当按下上或下箭头时触发
        float absH = Mathf.Abs(h);
        float absV = Mathf.Abs(v);

        if (absH <= 0.1 && absV <= 0.1)
        {
            h = m_lastInput.x;
            v = m_lastInput.z;
            absH = Mathf.Abs(absH);
            absV = Mathf.Abs(absV);
        }
        else
        {
            m_lastInput.x = h;
            m_lastInput.z = v;
        }

        if (absH <= 0.1 && absV <= 0.1)
            return Vector3.zero;

        if (absH >= absV)
            direction = new Vector3(h, 0, 0).normalized;
        else
            direction = new Vector3(0, 0, v).normalized;


        return direction;
    }

    public void OnGrid(Vector3 newPos)
    {
        Vector3 direction = GetMoveDirection();

        if (direction == Vector3.zero)
            return;


        if (!m_grid_move.WallCheck(direction))
            m_grid_move.SetDirection(direction);

    }
    public void Invincible()
    {
        m_player_state = STATE.INVINCIBLE;
        state_duration = 0;
    }
    public void Accelerate()
    {
        m_player_state = STATE.ACCELERATE;
        state_duration = 0;
        m_grid_move.ChangeSpeed(2);
    }
    public void Resurrection()
    {
        m_player_state = STATE.RESURRECTION;
    }
    public void DecreaseHp()
    {
        m_player_hp--;
        is_invincible = true;
        if (m_player_hp <= 0)
        {
            if (m_player_state == STATE.RESURRECTION)
            {
                m_grid_move.ChangeSpeed(2);
                m_player_state = STATE.ACCELERATE;
                m_player_hp = 1;
                state_duration = 25;
            }
            else
            {
                m_player_state = STATE.DEAD;
                GetComponent<CharaAnimator>().OnDead();
            }
        }
        else
        {
            m_grid_move.ChangeSpeed(2);
            m_player_state = STATE.ACCELERATE;
            state_duration = 25;
        }
    }
    public void Recovery()
    {
        if (m_player_hp < GameControl.MaxHp)
            m_player_hp = GameControl.MaxHp;
    }

    public void PlayerStart(int hp)
    {
        m_player_hp = hp;
        gameObject.AddComponent<Weapon>();
    }
    public void SetInvincible(bool temp)
    {
        is_invincible = temp;
        if (!temp)
        {
            is_invincible = true;
            m_player_state = STATE.INVINCIBLE;
            state_duration = 17;
        }

    }
    public bool CanHurt()
    {
        return is_invincible;
    }

    public int GetHp()
    {
        return m_player_hp;
    }

    public IEnumerator PlayPartical()
    {
        transform.GetChild(0).GetComponent<ParticleSystem>().Play();
        yield return new WaitForSeconds(0.5f);
        transform.GetChild(0).GetComponent<ParticleSystem>().Stop();
    }

    public void GetEffetct()
    {
        StartCoroutine(PlayPartical());
    }
}
